package kmp;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class leetCode1047_removeDuplicates {
    public String removeDuplicates(String s) {
        if(s == null || s.length() < 2) return s;
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            if(!stack.isEmpty() && s.charAt(i) == stack.peek()){
                stack.pop();
            }else{
                stack.add(s.charAt(i));
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        while (!stack.isEmpty()){
            stringBuilder.append(stack.pop());
        }
        return stringBuilder.reverse().toString();
    }

    public String removeDuplicates2(String s) {
        char[] array = new char[s.length()];
        int index = -1;
        for (char curChar : s.toCharArray()) {
            if(index >= 0 && array[index] == curChar){
                index--;
            }else{
                index++;
                array[index] = curChar;

            }
        }
        return new String(array, 0, index + 1);
    }

    public String removeDuplicates3(String s) {
        if(s== null || s.length() == 0) return s;
        char[] chars = s.toCharArray();
        int index = -1;
        char[] array = new char[s.length()];
        for (int i = 0; i < chars.length; i++) {
            if(index >= 0 && chars[index] == chars[i]){
                index--;
            }else{
                index++;
                array[index] = chars[i];
            }
        }

        return new String(array,0,index + 1);


    }
}
